﻿using Core.Web.Model;
using Core.DataAccess.Model.Project.Queue;
using Core.DataAccess.Model.Projects;
using Core.Framework.Model.Common;
using Core.Framework.Model.WebSockets;
using Core.Framework.Redis;
using Core.IBusiness.IComment;
using Core.IBusiness.IEntrepotModule;
using Core.IBusiness.IInformation;
using Core.IBusiness.ILoggerModule;
using Core.IBusiness.IOrderModule;
using Core.IBusiness.IProjectModule;
using Core.IBusiness.ISDK;
using Core.IBusiness.ISocketModule;
using Core.IBusiness.IUserModule;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace Core.Web
{
    /// <summary>
    /// Token 验证
    /// </summary>
    [ApiAuthorize]
    [ApiBaseController]
    public class BaseApiController : ControllerBase
    {

        public BaseApiController(
            IProject iProject = null,
            IProjectUserApiService iProjectUserApiService = null,
            IProjectUser iProjectUser = null,
            ISocketGroup iSocketGroup = null,
            ISocketRelationship iSocketRelationship = null,
            ISocketMessage iSocketMessage = null,
            ISocketUser iSocketUser = null,
            ILog iLog = null,
            IOrder iOrder = null,
            IOrderCoupon iOrderCoupon = null,
            IEntrepot iEntrepot = null,
            IEntrepotOperation iEntrepotOperation = null,
            IInformation iInformation = null,
            ICommentModule iComment = null,
            IPay iPay = null,
            ISMS iSMS = null,
            IQueue iQueue = null,
            IRedisHelper iRedis = null,
            ProjectContext iProjectContext = null,
            ProjectSocketContext iProjectSocketContext = null
            )
        {
            this.iProject = iProject;
            this.iProjectUserApiService = iProjectUserApiService;
            this.iProjectUser = iProjectUser;
            this.iSocketGroup = iSocketGroup;
            this.iSocketRelationship = iSocketRelationship;
            this.iSocketMessage = iSocketMessage;
            this.iSocketUser = iSocketUser;
            this.iLog = iLog;
            this.iOrder = iOrder;
            this.iOrderCoupon = iOrderCoupon;
            this.iEntrepot = iEntrepot;
            this.iEntrepotOperation = iEntrepotOperation;
            this.iInformation = iInformation;
            this.iComment = iComment;
            this.iPay = iPay;
            this.iSMS = iSMS;
            this.iQueue = iQueue;
            this.iRedis = iRedis;
            this.iProjectContext = iProjectContext;
            this.iProjectSocketContext = iProjectSocketContext;
        }


        /// <summary>
        /// 订单优惠卷
        /// </summary>
        protected IOrderCoupon iOrderCoupon { get; set; }


        /// <summary>
        /// Redis 缓存
        /// </summary>
        protected IRedisHelper iRedis { get; set; }

        /// <summary>
        /// 付款
        /// </summary>
        protected IPay iPay { get; set; }

        /// <summary>
        /// 短信
        /// </summary>
        protected ISMS iSMS { get; set; }

        /// <summary>
        /// 订单管理
        /// </summary>
        protected IOrder iOrder { get; set; }

        /// <summary>
        /// 消息队列
        /// </summary>
        protected IQueue iQueue { get; set; }

        /// <summary>
        /// 物流管理
        /// </summary>
        protected IEntrepot iEntrepot { get; set; }

        /// <summary>
        /// 产品运营管理
        /// </summary>
        protected IEntrepotOperation iEntrepotOperation { get; set; }

        /// <summary>
        /// 信息管理
        /// </summary>
        protected IInformation iInformation { get; set; }

        /// <summary>
        /// 评论管理
        /// </summary>
        protected ICommentModule iComment { get; set; }

        /// <summary>
        /// 项目日志
        /// </summary>
        protected ILog iLog { get; set; }

        /// <summary>
        /// 项目信息|操作
        /// </summary>
        protected IProject iProject { get; set; }

        /// <summary>
        /// 用户操作
        /// </summary>
        protected IProjectUser iProjectUser { get; set; }

        /// <summary>
        /// 项目Api操作
        /// </summary>
        protected IProjectUserApiService iProjectUserApiService { get; set; }

        /// <summary>
        /// 群组管理
        /// </summary>
        protected ISocketGroup iSocketGroup { get; set; }

        /// <summary>
        /// 用户关系管理
        /// </summary>
        protected ISocketRelationship iSocketRelationship { get; set; }

        /// <summary>
        /// Queue消息管理
        /// </summary>
        protected ISocketMessage iSocketMessage { get; set; }

        /// <summary>
        /// Queue 用户操作
        /// </summary>
        protected ISocketUser iSocketUser { get; set; }

        /// <summary>
        /// 项目管理数据库
        /// </summary>
        protected ProjectContext iProjectContext { get; set; }

        /// <summary>
        /// 消息管理数据库
        /// </summary>
        protected ProjectSocketContext iProjectSocketContext { get; set; }







        /// <summary>
        /// 客户端信息
        /// </summary>
        protected ApiClientInfo iClientInfo
        {
            get
            {
                var info = (ApiClientInfo)HttpContext.Items["ClientInfo"] ?? new ApiClientInfo();
                info.Ip = (string)HttpContext.Items["RemoteIpAddress"];
                return info;
            }
        }

        /// <summary>
        /// 项目信息
        /// </summary>
        protected ApiProjectInfo iProjectInfo
        {
            get
            {
                return (ApiProjectInfo)HttpContext.Items["ApiProjectInfo"] ?? new ApiProjectInfo();
            }
        }

        /// <summary>
        /// 项目用户信息
        /// </summary>
        protected ProjectUser iProjectUserInfo
        {
            get
            {
                return (ProjectUser)HttpContext.Items["ProjectUser"] ?? new ProjectUser();
            }
        }


        #region Common

        /// <summary>
        /// 通用 Result
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="result"></param>
        /// <param name="pagination">分页参数</param>
        /// <returns></returns>
        protected IResult Result<T>(MethodResult<T> result, Pagination pagination = null)
        {

            if (pagination == null)
            {
                return new ApiResult
                {
                    code = result.IsFinished ?
                        CodeResult.SUCCESS :
                        CodeResult.BUSINESS_ERROR,
                    info = result.IsFinished ? null : result.Discription,
                    date = result.Date
                };
            }
            else
            {
                return new ApiResult
                {
                    code = result.IsFinished ?
                        CodeResult.SUCCESS :
                        CodeResult.BUSINESS_ERROR,
                    info = result.IsFinished ? null : result.Discription,
                    date = new
                    {
                        pagination = pagination,
                        data = result.Date
                    }
                };
            }

        }

        /// <summary>
        /// 通用执行方法
        /// </summary>
        /// <param name="result"></param>
        /// <param name="isValids">验证模型</param>
        /// <returns></returns>
        protected IResult ExecMethod(
            Func<IResult> result, params Tuple<bool, string>[] isValids)
        {

            // 数据验证
            if (null != isValids && isValids.Length > 0)
                foreach (var isValid in isValids)
                {
                    if (!isValid.Item1)
                    {
                        return new ApiResult
                        {
                            code = CodeResult.DATA_IS_WRONG,
                            date = isValid.Item2
                        };
                    }
                }



            return result.Invoke();
        }

        #endregion

    }

}
